1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 import java.io.*;
139 import java.text.*;
140 import java.util.Locale;
141 import java.util.ResourceBundle;
142 import java.util.ResourceBundle.Control;
143 import java.util.MissingResourceException;
144
145 public class LocaleDataTest
146 {
147 public static void main(String[] args) throws Exception {
148
149
150
151
152 BufferedReader in = null;
153 PrintWriter out = null;
154 boolean writeNewFile = false;
155 boolean doThrow = true;
156
157 for (int i = 0; i < args.length; i++) {
158 if (args[i].equals("-w")) {
159 writeNewFile = true;
160 doThrow = false;
161 }
162
163 else if (args[i].equals("-nothrow"))
164 doThrow = false;
165
166 else if (args[i].equals("-s") && in == null)
167 in = new BufferedReader(new EscapeReader(new InputStreamReader(System.in,
168 "ISO8859_1")));
169 else if (!args[i].startsWith("-") && in == null)
170 in = new BufferedReader(new EscapeReader(new InputStreamReader(new
171 FileInputStream(args[i]), "ISO8859_1")));
172 }
173 if (in == null) {
174 File localeData = new File(System.getProperty("test.src", "."), "LocaleData");
175 in = new BufferedReader(new EscapeReader(new InputStreamReader(new
176 FileInputStream(localeData), "ISO8859_1")));
177 }
178 out = new PrintWriter(new EscapeWriter(new OutputStreamWriter(System.out,
179 "ISO8859_1")), true);
180
181
182 int errorCount = doTest(in, out, writeNewFile);
183
184
185
186 if (errorCount != 0) {
187 if (!writeNewFile)
188 out.println("Test failed. " + errorCount + " errors.");
189 if (doThrow)
190 throw new Exception("Test failed. " + errorCount + " errors.");
191 }
192 else if (!writeNewFile)
193 out.println("Test passed.");
194
195 in.close();
196 out.close();
197 }
198
199 static int doTest(BufferedReader in, PrintWriter out, boolean writeNewFile)
200 throws Exception {
201 int errorCount = 0;
202
203 String key = null;
204 String expectedValue = null;
205 String line = in.readLine();
206 while (line != null) {
207 if (line.startsWith("#") || line.length() == 0) {
208 if (writeNewFile)
209 out.println(line);
210 }
211
212 else {
213 int index = line.indexOf("=");
214 if (index == -1) {
215 key = line;
216 expectedValue = "";
217 }
218 else {
219 key = line.substring(0, index);
220 if (index + 1 == line.length())
221 expectedValue = "";
222 else
223 expectedValue = line.substring(index + 1);
224 }
225 if (!processLine(key, expectedValue, out, writeNewFile))
226 ++errorCount;
227 }
228 line = in.readLine();
229 }
230 return errorCount;
231 }
232
233 static boolean processLine(String key, String expectedValue, PrintWriter out,
234 boolean writeNewFile) throws Exception {
235 String rbName, localeName, resTag, qualifier;
236 String language = "", country = "", variant = "";
237 int index, oldIndex;
238
239 index = key.indexOf("/");
240 if (index == -1 || index + 1 == key.length())
241 throw new Exception("Malformed input file: no slashes in \"" + key + "\"");
242 rbName = key.substring(0, index);
243
244 oldIndex = index + 1;
245 index = key.indexOf("/", oldIndex);
246 if (index == -1 || index + 1 == key.length())
247 throw new Exception("Malformed input file: \"" + key + "\" is missing locale name");
248 localeName = key.substring(oldIndex, index);
249 boolean use_tag = localeName.indexOf("-") != -1;
250
251 if (use_tag == false && localeName.length() > 0) {
252 language = localeName.substring(0, 2);
253 if (localeName.length() > 3) {
254 country = localeName.substring(3, 5);
255 if (localeName.length() > 5)
256 variant = localeName.substring(6);
257 }
258 }
259
260 oldIndex = index + 1;
261 index = key.indexOf("/", oldIndex);
262 if (index == -1)
263 index = key.length();
264 resTag = key.substring(oldIndex, index);
265
266
267 if(resTag.endsWith("\\")) {
268 resTag = resTag.substring(0, resTag.length() - 1);
269 oldIndex = index;
270 index = key.indexOf("/", oldIndex + 1);
271 if (index == -1) index = key.length();
272 resTag += key.substring(oldIndex, index);
273 }
274
275 if (index < key.length() - 1)
276 qualifier = key.substring(index + 1);
277 else
278 qualifier = "";
279
280 String retrievedValue = null;
281 Object resource = null;
282 try {
283 String fullName = null;
284 if (rbName.equals("CalendarData")
285 || rbName.equals("CurrencyNames")
286 || rbName.equals("LocaleNames")
287 || rbName.equals("TimeZoneNames")) {
288 fullName = "sun.util.resources." + rbName;
289 } else {
290 fullName = "sun.text.resources." + rbName;
291 }
292 Locale locale;
293 if (use_tag) {
294 locale = Locale.forLanguageTag(localeName);
295 } else {
296 locale = new Locale(language, country, variant);
297 }
298 ResourceBundle bundle = ResourceBundle.getBundle(fullName,
299 locale,
300 ResourceBundle.Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
301 resource = bundle.getObject(resTag);
302 }
303 catch (MissingResourceException e) {
304 }
305
306 if (resource != null) {
307 if (resource instanceof String) {
308 retrievedValue = (String)resource;
309 }
310 else if (resource instanceof String[]) {
311 int element = Integer.valueOf(qualifier).intValue();
312 String[] stringList = (String[])resource;
313 if (element >= 0 || element < stringList.length)
314 retrievedValue = stringList[element];
315 }
316 else if (resource instanceof String[][]) {
317 String[][] stringArray = (String[][])resource;
318 int slash = qualifier.indexOf("/");
319 if (slash == -1) {
320 for (int i = 0; i < stringArray.length; i++) {
321 if (stringArray[i][0].equals(qualifier))
322 retrievedValue = stringArray[i][1];
323 }
324 }
325 else {
326 int row = Integer.valueOf(qualifier.substring(0, slash)).intValue();
327 int column = Integer.valueOf(qualifier.substring(slash + 1)).intValue();
328 if (row >= 0 || row < stringArray.length || column >= 0 || column <
329 stringArray[row].length)
330 retrievedValue = stringArray[row][column];
331 }
332 }
333 }
334
335 if (retrievedValue == null || !retrievedValue.equals(expectedValue)) {
336 if (retrievedValue == null)
337 retrievedValue = "<MISSING!>";
338
339 if (writeNewFile)
340 out.println(key + "=" + retrievedValue);
341 else {
342 out.println("Mismatch in " + key + ":");
343 out.println(" file = \"" + expectedValue + "\"");
344 out.println(" jvm = \"" + retrievedValue + "\"");
345 }
346 return false;
347 }
348 else {
349 if (writeNewFile)
350 out.println(key + "=" + expectedValue);
351 }
352 return true;
353 }
354 }
355
356 class EscapeReader extends FilterReader {
357 public EscapeReader(Reader in) {
358 super(in);
359 }
360
361 public int read() throws IOException {
362 if (buffer != null) {
363 String b = buffer.toString();
364 int result = b.charAt(0);
365 if (b.length() > 1)
366 buffer = new StringBuffer(b.substring(1));
367 else
368 buffer = null;
369 return result;
370 }
371 else {
372 int result = super.read();
373 if (result != '\\')
374 return result;
375 else {
376 buffer = new StringBuffer();
377 result = super.read();
378 buffer.append((char)result);
379 if (result == 'u') {
380 for (int i = 0; i < 4; i++) {
381 result = super.read();
382 if (result == -1)
383 break;
384 buffer.append((char)result);
385 }
386 String number = buffer.toString().substring(1);
387 result = Integer.parseInt(number, 16);
388 buffer = null;
389 return result;
390 }
391 return '\\';
392 }
393 }
394 }
395
396 public int read(char[] cbuf, int start, int len) throws IOException {
397 int p = start;
398 int end = start + len;
399 int c = 0;
400 while (c != -1 && p < end) {
401 c = read();
402 if (c != -1)
403 cbuf[p++] = (char)c;
404 }
405 if (c == -1 && p == start)
406 return -1;
407 else
408 return p - start;
409 }
410
411 private StringBuffer buffer = null;
412 }
413
414 class EscapeWriter extends FilterWriter {
415 public EscapeWriter(Writer out) {
416 super(out);
417 }
418
419 public void write(int c) throws IOException {
420 if ((c >= ' ' && c <= '\u007e') || c == '\r' || c == '\n')
421 super.write(c);
422 else {
423 super.write('\\');
424 super.write('u');
425 String number = Integer.toHexString(c);
426 if (number.length() < 4)
427 number = zeros.substring(0, 4 - number.length()) + number;
428 super.write(number.charAt(0));
429 super.write(number.charAt(1));
430 super.write(number.charAt(2));
431 super.write(number.charAt(3));
432 }
433 }
434
435 public void write(char[] cbuf, int off, int len) throws IOException {
436 int end = off + len;
437 while (off < end)
438 write(cbuf[off++]);
439 }
440
441 public void write(String str, int off, int len) throws IOException {
442 int end = off + len;
443 while (off < end)
444 write(str.charAt(off++));
445 }
446
447 private static String zeros = "0000";
448 }